package com.cb.myzhxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cb.myzhxy.mapper.TeacherMapper;
import com.cb.myzhxy.pojo.Admin;
import com.cb.myzhxy.pojo.LoginForm;
import com.cb.myzhxy.pojo.Teacher;
import com.cb.myzhxy.service.TeacherService;
import com.cb.myzhxy.util.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
*
*/
@Service("teacherServiceImpl")
@Transactional
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Override
    public Teacher login(LoginForm loginForm) {
        //创建QueryWrapper对象
        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
        //拼接查询条件
        queryWrapper.eq(Teacher::getName,loginForm.getUsername());
        // 转换成密文进行查询
        queryWrapper.eq(Teacher::getPassword, MD5.encrypt(loginForm.getPassword()));
        Teacher teacher = baseMapper.selectOne(queryWrapper);
        return teacher;
    }

    @Override
    public Teacher getTeacherById(Long userId) {
        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teacher::getId,userId);
        Teacher teacher = baseMapper.selectOne(queryWrapper);
        return teacher;
    }

    @Override
    public IPage<Teacher> getTeachersByOpr(Page<Teacher> page, Teacher teacher) {
        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
        if(teacher!=null){
            if(!StringUtils.isEmpty(teacher.getClazzName())){
                queryWrapper.eq(Teacher::getClazzName,teacher.getClazzName());
            }
            if(!StringUtils.isEmpty(teacher.getName())){
                queryWrapper.like(Teacher::getName,teacher.getName());
            }
            queryWrapper.orderByDesc(Teacher::getId);
            queryWrapper.orderByAsc(Teacher::getName);
        }
        Page<Teacher> page1 = baseMapper.selectPage(page, queryWrapper);
        return page1;
    }
}
